//
//  SXHFileMangaer.m
//  ZLZKBaseUtils
//
//  Created by sxh on 2020/10/3.
//

#import "SXHFileManager.h"
#import "macro.h"

@implementation SXHFileManager

+ (instancetype)sharedInstace {
    static SXHFileManager *shareInstace = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareInstace = [[SXHFileManager alloc] init];
    });
    return shareInstace;
}

//根据传入appid创建相关文件夹
- (void)createWorkPath:(NSString *)agreementName
             withAppid:(NSString *)appid {
    //依次创建doc download widget目录
    [self createFolderWithFile:kDocDirectory(agreementName,appid)];
    [self createFolderWithFile:kDownloadDirectory(agreementName,appid)];
    [self createFolderWithFile:kWidgetDirectory(agreementName,appid)];
//    [self createFolderWithFile:kBackupDirectory(appid)];
}

/**
 将bundle中widget对应的appid的目录文件拷贝到工作目录下的widget中 是否强制覆盖
 */

- (void)copyDirectoryToWorkPath:(NSString *)agreementName
                     withAppid:(NSString *)appid
                       isForce:(BOOL)force {
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //记录是否备份成功
    BOOL isBackup = NO;
    
    if (force) {
        
        [self copyFlolderFrom:kBundleWidgets(agreementName, appid) to:kWidgetDirectory(agreementName, appid)];
    }else {
        //先备份一份到备份目录
        if ([fileManager fileExistsAtPath:kWidgetDirectory(agreementName, appid)]) {
            
           isBackup = [self copyFlolderFrom:kWidgetDirectory(agreementName, appid) to:kBackupDirectory(agreementName, appid)];
        }
        //再复制
        BOOL isExit = [self copyFlolderFrom:kBundleWidgets(agreementName, appid) to:kWidgetDirectory(agreementName, appid)];
        if (!isExit) {
            NSLog(@"复制失败");
            //判断是否有备份 有则回退记录
            if (isBackup && [fileManager fileExistsAtPath:kBackupDirectory(agreementName, appid)]) {
                
                [self copyFlolderFrom:kBackupDirectory(agreementName, appid) to:kWidgetDirectory(agreementName, appid)];
            }
        }
    }
   
}


#pragma mark - 文件操作相关
//创建文件夹
- (void)createFolderWithFile:(NSString *)filePath {
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir;
    BOOL isExit = [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
    
    if (!isExit || !isDir)
    {
        [fileManager createDirectoryAtPath:filePath
               withIntermediateDirectories:YES
                                attributes:nil
                                     error:nil
         ];
    }
}

//复制文件夹
- (BOOL)copyFlolderFrom:(NSString *)filePath to:(NSString *)toPath {
    NSError *error;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if ([fileManager fileExistsAtPath:toPath]) {
        
        [fileManager removeItemAtPath:toPath error:&error];
    }
    return [fileManager copyItemAtPath:filePath toPath:toPath error:&error];
}

//移动文件夹
- (BOOL)moveFolderFrom:(NSString *)filePath to:(NSString *)toPath {
    NSError *error;
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if ([fileManager fileExistsAtPath:toPath]) {
        
        [fileManager removeItemAtPath:toPath error:&error];
    }
    return [fileManager moveItemAtPath:filePath toPath:toPath error:&error];
}

@end
